package com.jintoufs.web.action.finance;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.domain.account.Withdraw;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.account.WithdrawService;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.shop.ShopManagerService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenq
 */
@Controller
@RequestMapping("finance")
public class FinanceAction extends SupportAction{
    @Resource
    private AccountDetailService accountDetailService;
    @Resource
    private ShopService shopService;
    @Resource
    private WithdrawService withdrawService;
    @Resource
    private ShopManagerService shopManagerService;
    @Resource
    private CustomerService customerService;
    @RequestMapping("list")
    public String list( String valueId, Model model){
        model.addAttribute("valueId",valueId);
        return "/finance/list";
    }

    @RequestMapping("shop-list")
    public String shopList(@CurrentUser Customer customer){
        return "/finance/shop-list";
    }

    @RequestMapping("/withdraw-list")
    public String withdrawList() {
        return "finance/withdraw-list";
    }

    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters, String valueId, @CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        QueryRule queryRule;
        if (!ValidUtil.isEmpty(valueId)){
            queryRule = new QueryRule();
            queryRule.setField("sp.value_id");
            queryRule.setOp("eq");
            queryRule.setData(valueId);
            filters_.getRules().add(queryRule);
        }
        if (!ValidUtil.isEmpty(customer)){
            queryRule = new QueryRule();
            queryRule.setField("sm.customer_id");
            queryRule.setOp("eq");
            queryRule.setData(customer.getId());
            filters_.getRules().add(queryRule);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> list = accountDetailService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }
    @RequestMapping("/shop-query")
    @ResponseBody
    public JqgridPage platformQuery(int page, int rows, String sidx, String sord, String filters,@CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        QueryRule queryRule;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        if (!ValidUtil.isEmpty(customer)){
            queryRule = new QueryRule();
            queryRule.setField("c.id");
            queryRule.setOp("eq");
            queryRule.setData(customer.getId());
            filters_.getRules().add(queryRule);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String, Object>> list = accountDetailService.getPlatformByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }
    @RequestMapping("/withdraw-query")
    @ResponseBody
    public JqgridPage withdrawQuery(int page, int rows, String sidx, String sord, String filters, String valueId, @CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        QueryRule queryRule;
        if (!ValidUtil.isEmpty(customer)){
            queryRule = new QueryRule();
            queryRule.setField("customer_id");
            queryRule.setOp("eq");
            queryRule.setData(customer.getId());
            filters_.getRules().add(queryRule);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> list = withdrawService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }


    @RequestMapping("/withdraw")
    public String withdraw(Model model,String shopId,@CurrentUser Customer customer){

        model.addAttribute("shopId",shopId);
        return "finance/withdraw";
    }

    @RequestMapping("/doWithdraw")
    @ResponseBody
    public AjaxMsg doWithdraw(Withdraw withdraw,@CurrentUser Customer customer){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Shop shop = shopService.selectById(withdraw.getShopId());
        if (shop.getAccount().compareTo(withdraw.getPrice())>=0){
            withdraw.setCustomerId(customer.getId());
            withdraw.setId(getUUID());
            withdraw.setCreateTime(new Date());
            withdraw.setStatus(Constants.WITHDRAW_STATUS_NO);
            withdrawService.insert(withdraw);
            shop.setAccount(shop.getAccount().subtract(withdraw.getPrice()));
            shopService.save(shop,null);
            ajaxMsg.setMessage("申请成功");
        }else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("申请失败,可用金额不足");
        }
        return ajaxMsg;
    }

    @RequestMapping("getDetail")
    private String getDetail(String shopId,Model model){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map<String,Object> param = new HashMap<>();
//        param.put("shopId",shopId);
//        accountDetailService.getByParam(param);
        model.addAttribute("shopId",shopId);
        return "finance/detailList";
    }

    @RequestMapping("/detail")
    @ResponseBody
    public JqgridPage detail(int page, int rows, String sidx, String sord, String filters, String shopId, @CurrentUser Customer customer) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        QueryRule queryRule;
        if (!ValidUtil.isEmpty(shopId)){
            queryRule = new QueryRule();
            queryRule.setField("ad.shop_id");
            queryRule.setOp("eq");
            queryRule.setData(shopId);
            filters_.getRules().add(queryRule);
        }
        if (!ValidUtil.isEmpty(customer)){
            queryRule = new QueryRule();
            queryRule.setField("sm.customer_id");
            queryRule.setOp("eq");
            queryRule.setData(customer.getId());
            filters_.getRules().add(queryRule);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> list = accountDetailService.getDetailByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping("/toWithdraw")
    public String toWithdraw(@CurrentUser Customer customer, Model model){
        Map<String,Object> param = new HashMap<>();
        param.put("customerId",customer.getId());
        List<Map<String,Object>> maps = shopManagerService.queryByParam(param);
        model.addAttribute("customer",customer);
        model.addAttribute("list",true);
        model.addAttribute("shops",maps);
        return "finance/withdraw";
    }

}
